package me.send.business.service.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.send.business.base.ErrorEntity;
import me.send.business.base.Page;
import me.send.business.base.SendVariable;
import me.send.business.base.jpush.JPush;
import me.send.business.service.BaseService;
import me.send.business.service.cons.MessageCons;
import me.send.business.service.mapper.CommentMapper;
import me.send.business.service.mapper.UserMapper;
import me.send.business.service.pojo.Comment;
import me.send.business.service.pojo.Message;
import me.send.business.service.pojo.User;
import me.send.framework.util.HelperUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jpush.api.JPushClient;

@Service
public class CommentService extends BaseService {

	@Autowired
	private CommentMapper commentMapper;
	
	@Autowired
	private UserMapper userMapper;
	

	// 增加评论 2015年6月16日09:46:09
	public ErrorEntity comment_save(String devicenumber,Comment comment){
		ErrorEntity entity = error_1000();
		
		if (isDevicenumber(devicenumber)) {	
			if (comment!=null) {
				comment.setCreatetime(HelperUtil.getCurrentDate());
				// 设置下面的 查看状态
				comment.setLookstatus(0);
				if (comment.getPcommentid()<1) {
					comment.setPcommentid(0);
				}
				User u = userMapper.getUserNameById(comment.getUserid());
				JPushClient client = JPush.getUserJPushClient();
				if (comment.getPcommentid()!=0) {
					//给这条父评论人发消息
					User user = commentMapper.getUserByCommentParentId(comment.getPcommentid());
					String message = u.getNickname()+"对你发布的订单进行了评价";
					JPush.pushWithAlias(client, message, user.getIsvAccountId());
					Message msg = new Message(message,user.getUserid(),MessageCons.USER_TYPE_USER,MessageCons.MESSAGE_TYPE_COMMENT);
					msg.setOrderId(comment.getOrderid());
					msg.setFromUserId(u.getUserid());
					saveMessage(msg);
				}
				//给订单发布者发送消息
				User ownUser = userMapper.getUserIsvAccountIdByOrderId(comment.getOrderid());
				String message = u.getNickname()+"对你发布的订单进行了评价";
				JPush.pushWithAlias(client, message, ownUser.getIsvAccountId());
				Message msg = new Message(message,ownUser.getUserid(),MessageCons.USER_TYPE_USER,MessageCons.MESSAGE_TYPE_COMMENT);
				msg.setOrderId(comment.getOrderid());
				msg.setFromUserId(u.getUserid());
				saveMessage(msg);
				commentMapper.comment_save(comment);
			}else {
				entity = error_1006();
			}
		}else {
			entity = error_1002();
		}
		
		return entity;
	}
	
	public Map<String, Object> list_page_orderid(String devicenumber,
			Integer orderid, Page page) {
		Map<String, Object> map = new HashMap<String, Object>();
		ErrorEntity entity = error_1000();// 成功
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		if (isDevicenumber(devicenumber)) {
			if (orderid == null || orderid == 0) {
				entity = error_1006();
			} else {
				Map<String, Object> param = new HashMap<String, Object>();

				// 判断page是否为null
				if (page == null) {
					page = new Page();
				}

				param.put("page", page);
				param.put("orderid", orderid);

				list = commentMapper.list_page_orderid(param);
				entity = error_1000();
			}
		} else {
			entity = error_1002();
		}

		map.put(SendVariable.COMM_ERROR, entity);
		map.put(SendVariable.COMMON_LIST, list);

		return map;
	}
}
